// pages/lease/lease.js
import Dialog from '@vant/weapp/dialog/dialog';
import {baseURL, request} from"../../utils/request"
const goods = require("../../api/goods")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    zulinList: [
      {
      avatar: "",
      username: "",
      orderTime: "",
      orderId: "",
      status: "",
      orderDetailList: [],
      imageUrls: "",
      showPrices: "",
      }
    ],
    baseURL: baseURL,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAppointmentZuLinList();
  },

  //根据用户id查询名下所有租赁信息
  getAppointmentZuLinList(){
    goods.getOrder(wx.getStorageSync("userInfo").userId).then(res=>{
      if(res.code == 200){
        console.log("userInfo",res)
        let zulinList = [];
        for (let i = 0; i < res.data.length; i++) {
          zulinList.push({
            avatar: wx.getStorageSync("userInfo").imageUrl,
            username: wx.getStorageSync("userInfo").username,
            orderTime: res.data[i].orderTime,
            orderId: res.data[i].orderId,
            status: res.data[i].status,
            orderDetailList: res.data[i].orderDetailList,
            imageUrls: res.data[i].qrcode,
          })
        }
        this.setData({zulinList:zulinList})
        console.log("zulinList:",this.data.zulinList)
      }
    })
  },
  //核销租赁
  verification(event){
    var orderId = event.currentTarget.dataset.orderId;
    console.log("event:",orderId)
    Dialog.confirm({
      title: '核销租赁',
      message: '确定要将商品取消租赁？',
    })
    .then(() => {
      goods.delOrder(orderId).then(res => {
        if (res.code == 200) {
          Dialog.confirm({
            title: '核销成功',
            message: '感谢使用，动动发财的小手，点个赞吧！',
          }).then(()=>{})
        }
      })
    })
    .catch(() => {
      // on cancel
    });
  },
  togglePriceDetails(event) {
    const index = event.currentTarget.dataset.index;
    const updatedList = this.data.zulinList.map((item, i) => {
      if (i === parseInt(index)) {
        return { ...item, showPrices: !item.showPrices };
      }
      return item;
    });
    this.setData({ zulinList: updatedList });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})